// pages/find/releaseNews/releaseNews.js
let app = getApp()
var util = require('../../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: null, // type发布类型  //1 评论文章   //2瞬间评论一级评论    //3  发布动态    //4评论服务     //5评论动态   //6  动态评论一级评论
    momentId: null,
    commentId: null,
    dynamicId: null, //5
    message: null,

    addForm: {
      message: null,
      backImage: [], //图片列表
    },

    placeholder: '', // 美好的生活片刻，值得用文字记录下来...
    api: util.config().api + '/common/upload',
    videoApi: util.config().api + '/common/uploadVideo',
    fileListfrontImage: [],
    fileListVideo: [],
    coverImg: [], //封面
    radio: '0', //动态类型


    //4评论服务
    // "evaluate": "string",//评价内容
    orderId: null, //订单号
    "parentValuateId": 0, //先不传
    value: 5 // 星级


  },
  //现在发布
  releaseNow() {
    let type = this.data.type
    if (!!!this.data.addForm.message) {
      // !!!this.data.addForm.message && this.data.addForm.backImage.length == 0
      wx.showToast({
        title: '内容为空',
        image: '../../../assets/error.png'
      })
    } else if (type == 3 && this.data.radio == 0 && this.data.addForm.backImage.length <= 0) {
      wx.showToast({
        title: '图片为空',
        image: '../../../assets/error.png'
      })
    } else if (type == 3 && this.data.radio == 1 && this.data.addForm.backImage.length <= 0) {
      wx.showToast({
        title: '视频为空',
        image: '../../../assets/error.png'
      })
    } else {
      if (type == 1) { //1 评论文章
        var postObj = {
          url: '/mobileUser/comment',
          method: 'post',
          data: {
            momentId: this.data.momentId,
            evaluate: this.data.addForm.message,
            // parentCommentId:'',  //文章不需要传 
            url: this.data.addForm.backImage
          }
        }
        app.request.wxRequest(postObj).then(res => {
          if (res.data.code === 200) {
            wx.showToast({
              title: '评论成功',
              image: '../../../assets/success.png',
              duration: 500,
              mask: true,
              success: () => {
                setTimeout(() => {
                  wx.navigateBack({
                    delta: -1,
                  })
                }, 500)
              }
            })
          }
        })
      } else if (type == 2) { //2评论一级评论
        var postObj = {
          url: '/mobileUser/comment',
          method: 'post',
          data: {
            parentCommentId: this.data.commentId,
            evaluate: this.data.addForm.message,
            // parentCommentId:'',  //文章不需要传 
            // url: this.data.addForm.backImage
          }
        }
        app.request.wxRequest(postObj).then(res => {
          if (res.data.code === 200) {
            wx.showToast({
              title: '评论成功',
              image: '../../../assets/success.png',
              duration: 500,
              mask: true,
              success: () => {
                setTimeout(() => {
                  wx.navigateBack({
                    delta: -1,
                  })
                }, 500)
              }
            })
          }
        })
      } else if (type == 3) { //3  发布动态
        var data = null
        if (this.data.radio == 0) {
          data = {
            momentType: this.data.radio,
            detail: this.data.addForm.message,
            urls: this.data.addForm.backImage,
          }
        }

        if (this.data.radio == 1) {
          data = {
            momentType: this.data.radio,
            detail: this.data.addForm.message,
            videoUrl: this.data.addForm.backImage[0],
            urls: this.data.coverImg,
          }
        }
        var postObj = {
          url: '/mobileUser/dynamic',
          method: 'post',
          data: data
        }
        app.request.wxRequest(postObj).then(res => {
          if (res.data.code === 200) {
            wx.showToast({
              title: '发布成功',
              image: '../../../assets/success.png',
              duration: 500,
              mask: true,
              success: () => {
                setTimeout(() => {
                  wx.navigateBack({
                    delta: -1,
                  })
                }, 500)
              }
            })
          }
        })
      } else if (type == 4) { //4评论服务
        // POST/mobileUser/evaluate新增订单评价
        var postObj = {
          url: '/mobileUser/evaluate',
          method: 'post',
          data: {
            orderId: this.data.orderId,
            evaluate: this.data.addForm.message,
            value: this.data.value
          }
        }
        app.request.wxRequest(postObj).then(res => {
          if (res.data.code === 200) {
            wx.showToast({
              title: '发布成功',
              image: '../../../assets/success.png',
              duration: 500,
              mask: true,
              success: () => {
                setTimeout(() => {
                  wx.navigateBack({
                    delta: -1,
                  })
                }, 500)
              }
            })
          }
        })

      } else if (type == 5) { //评论动态
        var postObj = {
          url: '/mobileUser/dynamicComment',
          method: 'post',
          data: {
            dynamicId: this.data.dynamicId,
            evaluate: this.data.addForm.message
          }
        }
        app.request.wxRequest(postObj).then(res => {
          if (res.data.code === 200) {
            wx.showToast({
              title: '评论成功',
              image: '../../../assets/success.png',
              duration: 500,
              mask: true,
              success: () => {
                setTimeout(() => {
                  wx.navigateBack({
                    delta: -1,
                  })
                }, 500)
              }
            })
          }
        })
      }else if (type == 6) { //6  动态评论一级评论
        var postObj = {
          url: '/mobileUser/dynamicComment',
          method: 'post',
          data: {
            parentCommentId: this.data.commentId,
            evaluate: this.data.addForm.message
          }
        }
        app.request.wxRequest(postObj).then(res => {
          if (res.data.code === 200) {
            wx.showToast({
              title: '评论成功',
              image: '../../../assets/success.png',
              duration: 500,
              mask: true,
              success: () => {
                setTimeout(() => {
                  wx.navigateBack({
                    delta: -1,
                  })
                }, 500)
              }
            })
          }
        })
      }
    }

  },
  // 输入文字
  onChange(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    const message = event.detail
    this.setData({
      [`addForm.message`]: message
    })
  },
  //星级
  onChangevalue(event) {
    console.log('星级', event.detail);

    this.setData({
      value: event.detail,
    });
  },
  // 上传后校验    图片
  afterReadfrontImage(event) {
    let that = this
    const {
      file
    } = event.detail;
    console.log(file);

    wx.uploadFile({
      url: that.data.api, // 仅为示例，非真实的接口地址
      filePath: file.path,
      name: 'file',
      formData: {
        user: 'test'
      },
      success(res) {
        // 上传完成需要更新 fileList
        var data = JSON.parse(res.data)
        console.log('图片上传服务器返回值', data)
        var fileList = that.data.fileListfrontImage
        fileList.push(data)
        var backImage = that.data.addForm.backImage
        backImage.push(data.fileName)
        that.setData({
          fileListfrontImage: fileList,
          'addForm.backImage': backImage
        });

      },
    });

  },
  // 上传前校验   图片
  beforeRead(event) {
    const {
      file,
      callback
    } = event.detail;
    callback(file.type === 'image');
  },
  // 删除    图片
  deleteReadfrontImage(e) {
    console.log(e);
    var index = +e.detail.index
    // 获取下标删
    // this.setData({
    //   fileListfrontImage: [],
    //   'addForm.frontImage': ''
    // })
    var fileListfrontImage = this.data.fileListfrontImage
    var backImage = this.data.addForm.backImage
    fileListfrontImage.splice(index, 1)
    backImage.splice(index, 1)
    this.setData({
      fileListfrontImage: fileListfrontImage,
      'addForm.backImage': backImage
    })
    console.log('fileListfrontImage', this.data.fileListfrontImage);
    console.log('addForm', this.data.addForm);

  },





  // 上传后校验    视频
  afterReadvideo(event) {
    let that = this
    const {
      file
    } = event.detail;
    console.log(file);

    wx.uploadFile({
      url: that.data.videoApi, // 仅为示例，非真实的接口地址
      filePath: file.path,
      name: 'file',
      formData: {
        user: 'test'
      },
      success(res) {
        // 上传完成需要更新 fileList
        var data = JSON.parse(res.data)
        console.log('图片上传服务器返回值', data)
        if (data.code == 200) {
          wx.showToast({
            title: '视频上传成功成功',
            image: '../../../assets/success.png',
            duration: 500,
            mask: true
          })
        }
        var fileList = that.data.fileListfrontImage
        fileList.push(data)
        var backImage = that.data.addForm.backImage
        backImage.push(data.data.fileName)
        var urls = []
        urls.push(data.data.picture)
        that.setData({
          fileListfrontImage: fileList,
          'addForm.backImage': backImage,
          coverImg: urls
        });

      },
    });

  },
  // 上传前校验   视频
  beforeReadvideo(event) {
    const {
      file,
      callback
    } = event.detail;
    callback(file.type === 'video');
  },
  // 删除    视频
  deleteReadvideo(e) {
    console.log(e);
    var index = +e.detail.index
    var fileListfrontImage = this.data.fileListfrontImage
    var backImage = this.data.addForm.backImage
    fileListfrontImage.splice(index, 1)
    backImage.splice(index, 1)
    this.setData({
      fileListfrontImage: fileListfrontImage,
      'addForm.backImage': backImage
    })
    console.log('fileListfrontImage', this.data.fileListfrontImage);
    console.log('addForm', this.data.addForm);

  },

  // 切换动态类型
  onChangeradio(event) {
    this.setData({
      radio: event.detail,
    });
    this.setData({
      fileListfrontImage: [],
      'addForm.backImage': []
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    if (options.type == 1) { //1 评论文章
      this.setData({
        placeholder: "写评价~",
        type: 1,
        momentId: options.momentId
      })
      wx.setNavigationBarTitle({
        title: '写评价',
      })
    } else if (options.type == 2) { //2评论一级评论
      this.setData({
        placeholder: "回复评论~",
        type: 2,
        commentId: options.commentId
      })
      wx.setNavigationBarTitle({
        title: '回复评论',
      })
    } else if (options.type == 3) {
      this.setData({
        placeholder: "发布动态~",
        type: 3,
      })
      wx.setNavigationBarTitle({
        title: '发布动态',
      })
    } else if (options.type == 4) {
      this.setData({
        placeholder: "评论服务~",
        type: 4,
        orderId: options.orderCode
      })
      wx.setNavigationBarTitle({
        title: '评论服务',
      })
    } else if (options.type == 5) {
      this.setData({
        placeholder: "评论动态~",
        type: 5,
        dynamicId: options.dynamicId
      })
      wx.setNavigationBarTitle({
        title: '评论动态',
      })
    } else if (options.type == 6) { //动态评论一级评论
      this.setData({
        placeholder: "回复评论~",
        type: 6,
        commentId: options.commentId
      })
      wx.setNavigationBarTitle({
        title: '回复评论',
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})